/*
 * dept01 table ��� Ŭ����
 * -crud
 * 1.���˻�
 * 		select * from dept01;
 * 2.���Ǻ� �˻�
 * 		deptno�� �̸� �μ��� ��ġ
 * 		select dname, loc from dept01 where deptno=??
 * 3.����
 * 		insert into dept01 valuse( , , );
 * 4.����
 * 		update dept01 set  loc =?  where deptno =?
 * 5.����
 * 		delete from dept01 where deptno = ? 
 */

package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.domain.Dept01DTO;
import model.util.DBUtil;
/*
 * ������ ����Ǽ�
 * 1.�����ȯ
 * 	-�����Ͱ� ������ ��� ArrayList�� Dept01DTO ��ü ������ ��ȯ -->SuccessView.java
 * 	-������ �� ����� ArrayList �� null   -->SuccessView.java ���� ��ٰ� ���
 * 2. ������
 * 	-exception    -->FailView.java
 */

public class Dept01DAO {
	//�̱��� ����?/
	//��ü �˻�
	public static ArrayList<Dept01DTO> selectDept01s() throws SQLException{ 
		Connection con = null;
		Statement stmt = null;
		ResultSet rset = null;
		ArrayList<Dept01DTO> allData = null;
		if(con == null){
			System.out.println("test code2");
      System.out.println("testtestest ddd uuu");
		}
		
		try{
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			String query = "select * from dept01";
			rset = stmt.executeQuery(query);
			 
			allData = new ArrayList<Dept01DTO>();
			while(rset.next()){
				allData.add(new Dept01DTO(rset.getInt(1),rset.getString(2),rset.getString(3)));
			}
		}catch(SQLException s){
			s.printStackTrace();
			throw s;
		}finally{
			DBUtil.close(con,stmt,rset);
		}
		return allData;
	}
	 
	//2. ���Ǻ� �˻�
	//select dname loc from dept01 where deptno
	public static Dept01DTO getDept(int deptno) throws SQLException{
		Connection con = null;
		Statement stmt = null;
		ResultSet rset = null;
		Dept01DTO data = null;
		try{
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			String query = "select dname, loc from dept01 where deptno = 30";
			rset = stmt.executeQuery(query);
			if(rset.next()){
				data = new Dept01DTO(deptno,rset.getString(1),rset.getString(2));    //deptno dho �־����?? ���ο� ��ü ��?
			}	 
		}catch(SQLException s){
			s.printStackTrace();
			throw s;
		}finally{
			DBUtil.close(con,stmt,rset);
		}
		return data;
	}
	
	//insert                 //StringBuilder ���
	public static void insert(Object object) {
		Connection con = null;
		Statement stmt = null;
	//	System.out.println("---" + ((Dept01DTO) object).getDeptno());
		try{
			con = DBUtil.getConnection();
			stmt = con.createStatement();    //���⼭ ��ȯ �� �ϴ� ��???
			String sql = "insert into dept01 values"+"("+((Dept01DTO) object).getDeptno()+",'"+((Dept01DTO) object).getDname()+"','"+((Dept01DTO) object).getLoc()+"')";
			int i = stmt.executeUpdate(sql);
		}catch(SQLException s){
			s.printStackTrace();
		}finally{
			DBUtil.close(con,stmt);
		}
	}
	//update update dept01 set  loc =?  where deptno =?
	public static void update(Object object){
		Connection con = null;
		Statement stmt = null;
	//	System.out.println("---" + ((Dept01DTO)object).getLoc());
		try{
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			String sql = "update Dept01 set loc = '"+((Dept01DTO)object).getLoc()+"' where deptno="+((Dept01DTO) object).getDeptno();
			int i = stmt.executeUpdate(sql);		 
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, stmt);
		}
	}
	//delete
	public static void delete(int deptno){
		Connection con = null;
		Statement stmt = null;
		try{
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			String sql = "delete from dept01 where deptno ="+deptno;
			int i = stmt.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(con, stmt);
		}
	}
}

